package br.com.edumobi.fragments;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;

import br.com.edumobi.P;
import br.com.edumobi.R;
import br.com.edumobi.dados.modelos.Aluno;
import br.com.edumobi.dados.modelos.enuns.UF;
import br.com.edumobi.utils.Mask;

public class EnderecoAlunoFragment extends GenericoFragment {

    private TextView textViewLogradouroEnderecoAlunoFragment;
    private TextView textViewNumeroEnderecoAlunoFragment;
    private TextView textViewComplementoEnderecoAlunoFragment;
    private TextView textViewBairroEnderecoAlunoFragment;
    private TextView textViewCepEnderecoAlunoFragment;
    private TextView textViewCidadeEnderecoAlunoFragment;
    private TextView textViewUfsEnderecoAlunoFragment;
    private EditText editTextLogradouroEnderecoAlunoFragment;
    private EditText editTextNumeroEnderecoAlunoFragment;
    private EditText editTextComplementoEnderecoAlunoFragment;
    private EditText editTextBairroEnderecoAlunoFragment;
    private EditText editTextCepEnderecoAlunoFragment;
    private EditText editTextCidadeEnderecoAlunoFragment;
    private Spinner spinnerUfsEnderecoAlunoFragment;
    private Button buttonSalvarEnderecoAlunoFragment;
    private ProgressBar progressBarEnderecoAlunoFragment;

    private AcoesEnderecoAlunoFragment activity;
    private EnderecoAlunoTask task;

    private Aluno aluno;

    View.OnClickListener clickButtonSalvarEnderecoAlunoFragment = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String logradouro = editTextLogradouroEnderecoAlunoFragment.getText().toString();
            String numero = editTextNumeroEnderecoAlunoFragment.getText().toString();
            String complemento = editTextComplementoEnderecoAlunoFragment.getText().toString();
            String bairro = editTextBairroEnderecoAlunoFragment.getText().toString();
            String cep = editTextCepEnderecoAlunoFragment.getText().toString();
            String cidade = editTextCidadeEnderecoAlunoFragment.getText().toString();

            if (activity.conectado()) {
                aluno.endereco.logradouro = logradouro;
                aluno.endereco.numero = numero;
                aluno.endereco.complemento = complemento;
                aluno.endereco.bairro = bairro;
                aluno.endereco.cep = cep;
                aluno.endereco.cidade.nome = cidade;
                aluno.endereco.cidade.uf = UF.getEnum((String) spinnerUfsEnderecoAlunoFragment.getSelectedItem());

                task = new EnderecoAlunoTask();
                task.execute();
            } else {
                activity.falarEExibirTexto(R.string.excecao_sem_conexao_internet);
            }
        }
    };

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        activity = (AcoesEnderecoAlunoFragment) getActivity();

        aluno = activity.obterAluno();

        editTextLogradouroEnderecoAlunoFragment.setText(aluno.endereco != null ? aluno.endereco.logradouro : P.STRING_EMPITY);
        editTextNumeroEnderecoAlunoFragment.setText(aluno.endereco != null ? aluno.endereco.numero : P.STRING_EMPITY);
        editTextBairroEnderecoAlunoFragment.setText(aluno.endereco != null ? aluno.endereco.logradouro : P.STRING_EMPITY);
        editTextCepEnderecoAlunoFragment.setText(aluno.endereco != null ? aluno.endereco.cep : P.STRING_EMPITY);
        editTextCidadeEnderecoAlunoFragment.setText(aluno.endereco != null && aluno.endereco.cidade != null ? aluno.endereco.cidade.nome : P.STRING_EMPITY);
        spinnerUfsEnderecoAlunoFragment.setSelection(aluno.endereco != null && aluno.endereco.cidade != null && aluno.endereco.cidade.uf != null ? aluno.endereco.cidade.uf.id : 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_endereco_aluno, container, false);

        textViewLogradouroEnderecoAlunoFragment = (TextView) view.findViewById(R.id.textViewLogradouroEnderecoAlunoFragment);
        textViewNumeroEnderecoAlunoFragment = (TextView) view.findViewById(R.id.textViewNumeroEnderecoAlunoFragment);
        textViewComplementoEnderecoAlunoFragment = (TextView) view.findViewById(R.id.textViewComplementoEnderecoAlunoFragment);
        textViewBairroEnderecoAlunoFragment = (TextView) view.findViewById(R.id.textViewBairroEnderecoAlunoFragment);
        textViewCepEnderecoAlunoFragment = (TextView) view.findViewById(R.id.textViewCepEnderecoAlunoFragment);
        textViewCidadeEnderecoAlunoFragment = (TextView) view.findViewById(R.id.textViewCidadeEnderecoAlunoFragment);
        textViewUfsEnderecoAlunoFragment = (TextView) view.findViewById(R.id.textViewUfsEnderecoAlunoFragment);
        editTextLogradouroEnderecoAlunoFragment = (EditText) view.findViewById(R.id.editTextLogradouroEnderecoAlunoFragment);
        editTextNumeroEnderecoAlunoFragment = (EditText) view.findViewById(R.id.editTextNumeroEnderecoAlunoFragment);
        editTextComplementoEnderecoAlunoFragment = (EditText) view.findViewById(R.id.editTextComplementoEnderecoAlunoFragment);
        editTextBairroEnderecoAlunoFragment = (EditText) view.findViewById(R.id.editTextBairroEnderecoAlunoFragment);
        editTextCepEnderecoAlunoFragment = (EditText) view.findViewById(R.id.editTextCepEnderecoAlunoFragment);
        editTextCidadeEnderecoAlunoFragment = (EditText) view.findViewById(R.id.editTextCidadeEnderecoAlunoFragment);
        spinnerUfsEnderecoAlunoFragment = (Spinner) view.findViewById(R.id.spinnerUfsEnderecoAlunoFragment);
        buttonSalvarEnderecoAlunoFragment = (Button) view.findViewById(R.id.buttonSalvarEnderecoAlunoFragment);
        progressBarEnderecoAlunoFragment = (ProgressBar) view.findViewById(R.id.progressBarEnderecoAlunoFragment);

        editTextCepEnderecoAlunoFragment.addTextChangedListener(Mask.insert("#####-###", editTextCepEnderecoAlunoFragment));
        buttonSalvarEnderecoAlunoFragment.setOnClickListener(clickButtonSalvarEnderecoAlunoFragment);

        if (task != null && task.getStatus().equals(AsyncTask.Status.RUNNING)) {
            esconderComponentes();
            progressBarEnderecoAlunoFragment.setVisibility(View.VISIBLE);
        }
        return view;
    }

    @Override
    public void exibirComponentes() {
        textViewLogradouroEnderecoAlunoFragment.setVisibility(View.VISIBLE);
        textViewNumeroEnderecoAlunoFragment.setVisibility(View.VISIBLE);
        textViewComplementoEnderecoAlunoFragment.setVisibility(View.VISIBLE);
        textViewBairroEnderecoAlunoFragment.setVisibility(View.VISIBLE);
        textViewCepEnderecoAlunoFragment.setVisibility(View.VISIBLE);
        textViewUfsEnderecoAlunoFragment.setVisibility(View.VISIBLE);
        textViewCidadeEnderecoAlunoFragment.setVisibility(View.VISIBLE);
        editTextLogradouroEnderecoAlunoFragment.setVisibility(View.VISIBLE);
        editTextNumeroEnderecoAlunoFragment.setVisibility(View.VISIBLE);
        editTextComplementoEnderecoAlunoFragment.setVisibility(View.VISIBLE);
        editTextBairroEnderecoAlunoFragment.setVisibility(View.VISIBLE);
        editTextCepEnderecoAlunoFragment.setVisibility(View.VISIBLE);
        editTextCidadeEnderecoAlunoFragment.setVisibility(View.VISIBLE);
        spinnerUfsEnderecoAlunoFragment.setVisibility(View.VISIBLE);
        buttonSalvarEnderecoAlunoFragment.setVisibility(View.VISIBLE);
        progressBarEnderecoAlunoFragment.setVisibility(View.VISIBLE);
    }

    @Override
    public void esconderComponentes() {
        textViewLogradouroEnderecoAlunoFragment.setVisibility(View.GONE);
        textViewNumeroEnderecoAlunoFragment.setVisibility(View.GONE);
        textViewComplementoEnderecoAlunoFragment.setVisibility(View.GONE);
        textViewBairroEnderecoAlunoFragment.setVisibility(View.GONE);
        textViewCepEnderecoAlunoFragment.setVisibility(View.GONE);
        textViewUfsEnderecoAlunoFragment.setVisibility(View.GONE);
        textViewCidadeEnderecoAlunoFragment.setVisibility(View.GONE);
        editTextLogradouroEnderecoAlunoFragment.setVisibility(View.GONE);
        editTextNumeroEnderecoAlunoFragment.setVisibility(View.GONE);
        editTextComplementoEnderecoAlunoFragment.setVisibility(View.GONE);
        editTextBairroEnderecoAlunoFragment.setVisibility(View.GONE);
        editTextCepEnderecoAlunoFragment.setVisibility(View.GONE);
        editTextCidadeEnderecoAlunoFragment.setVisibility(View.GONE);
        spinnerUfsEnderecoAlunoFragment.setVisibility(View.GONE);
        buttonSalvarEnderecoAlunoFragment.setVisibility(View.GONE);
        progressBarEnderecoAlunoFragment.setVisibility(View.GONE);
    }

    private class EnderecoAlunoTask extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPreExecute() {
            esconderComponentes();
            progressBarEnderecoAlunoFragment.setVisibility(View.VISIBLE);
            activity.falarTexto(R.string.aguarde);
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            return activity.salvarDadosAluno(aluno);
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            exibirComponentes();
            progressBarEnderecoAlunoFragment.setVisibility(View.GONE);

            if (aBoolean) {
                activity.falarEExibirTexto(R.string.dados_salvos_sucesso);
                activity.salvarPreferencesAluno(aluno);
            } else {
                activity.falarEExibirTexto(R.string.excecao_atualizacao_dados_aluno_malsucedida);
            }
        }
    }

    public interface AcoesEnderecoAlunoFragment extends AcoesGenericoFragment{
        Aluno obterAluno();
        Boolean salvarDadosAluno(Aluno aluno);
    }
}
